package application;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;

import vo.Point;
import vo.User;
import JDBC.DatabaseDao;
import Service.UserService;
import Utils.Constants;
import Utils.FileUtils;
import Utils.MathUtils;
import Utils.SortUtils;
import dao.UserDao;

public class GraphModel {

	public static Hashtable<String, Double> getGenderModel() {
		Hashtable<String, Double> gender_percentage = new Hashtable<String, Double>();
		int count = UserService.getCount();
		int maleCount = UserService.getMaleCount();
		double malePercentage = MathUtils.precision((double) maleCount / count, Constants.PRECISION);
		gender_percentage.put("male", malePercentage);
		gender_percentage.put("female", 1 - malePercentage);
		return gender_percentage;
	}

	public static Hashtable<String, Double> getVerifiedModel() {
		Hashtable<String, Double> verified_percentage = new Hashtable<String, Double>();
		int count = UserService.getCount();
		int verifiedCount = UserService.getVerifiedCount();
		double verifiedPercentage = MathUtils.precision((double) verifiedCount / count, Constants.PRECISION);
		verified_percentage.put("verified", verifiedPercentage);
		verified_percentage.put("unverified", 1 - verifiedPercentage);
		return verified_percentage;
	}

	/**
	 * 在散点图上显示一个用户的（属性1,属性2）点
	 * 
	 * @return
	 */

	public static ArrayList<Point> getUserPropVSUserPropModel(String prop1, String prop2) {
		ArrayList<Point> points = new ArrayList<Point>();
		try {
			String sql = "select " + prop1 + "," + prop2 + " from users";
			System.out.println(sql);
			Statement stmt = DatabaseDao.getConnection().createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				double propValue1 = rs.getDouble(prop1);
				double propValue2 = rs.getDouble(prop2);
				Point point = new Point(propValue1, propValue2);
				points.add(point);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return points;
	}

	/**
	 * 显示各种属性在users中的分布， 有key条微博的人数有value个 entity取值
	 * ：followers,friends,statuses,favourites
	 * 
	 * @param entity
	 * @return
	 */
	public static ArrayList<Point> getFVSFFModel(String entity) {
		ArrayList<Point> F_FF = new ArrayList<Point>();
		try {
			String sql = "select * from " + entity + "_distribution";
			Statement stmt = DatabaseDao.getConnection().createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				double freq = rs.getDouble("freq");
				double freq_freq = rs.getDouble("freq_freq");
				Point point = new Point(freq,freq_freq);
				F_FF.add(point);
			}
			System.out.println("多少个点:" + F_FF.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return F_FF;
	}

	public static void getBordaRankingModel() {
		String result = "";
		Hashtable<String, Double> userId_borda = FileUtils.readHashtableD("userId_borda.txt");
		Map.Entry[] entries = SortUtils.sortedHashtableByValue(userId_borda, Constants.DESC);
		for (int i = 0; i < entries.length; i++) {
			String userId = (String) entries[i].getKey();
			double userIndex = i;
			double borda = (Double) entries[i].getValue();
			User user = UserDao.getById(userId);
			int followersCount = user.getFollowsCount();
			int statusesCount = user.getStatusesCount();
			result += userIndex + "," + borda + "," + followersCount + "," + statusesCount + "\n";
		}
		FileUtils.write("ranking_results.txt", result);
	}

	// TODO: 显示各种排名的model
	public static void main(String ars[]) {
		// UserService.insertFollowerFVSFF();
		// UserService.insertStatusFVSFF();
		getBordaRankingModel();
	}
}
